#!/usr/bin/env python3

import csv
import requests
from time import sleep
from collections import defaultdict

url = "http://api.map.baidu.com/place/v2/search"


def search(city, query):
  count = 0
  page_num = 0
  page_size = 20
  li = []
  while 1:
    print(">>", city, page_num)
    r = requests.get(
      url,
      dict(
        query=query,
        region=city,
        output="json",
        ak="wSmlLicE6NgPF7HSVWkNBYML45wkCSrZ",
        # ak="etlhRmrtQ4yjabMZoXkzBGKkchttl6Ef",
        page_num=page_num,
        page_size=page_size,
      )
    ).json()
    # print(r)
    if r['status'] == 401:
      print(r)
      sleep(1)
      continue
    results = 'results'
    if results not in r:
      print(r)
    for i in r[results]:
      if 'location' in i:
        if 'address' in i:
          li.append([i['name'], i['address']])
        else:
          print(i)
    sleep(1)
    count += page_size
    if count < r['total']:
      page_num += 1
    else:
      break
  return li


def main(query):
  city_shop = defaultdict(set)
  with open('city.txt', 'r') as f:
    for i in csv.reader(f):
      city = i[1]
      # print(">>",city)
      # city = '北京市'
      li = search(city, query)
      total = len(li)
      if total:
        r = city_shop[city]
        for j in li:
          r.add(tuple(j))
          print("\t" + "\t".join(j))
      print("合计", total, "\n")
      # sleep(1)

  with open(
    'data/' + query + '.csv',
    mode='w'
  ) as out:
    c = csv.writer(out)
    for k, v in city_shop.items():
      for i in v:
        t = list(i)
        t.insert(1, k)
        c.writerow(t)


def run():
  li = "酸奶".split(" ")
  for query in li:
    main(query)


run()
